package com.xiaojian.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaojian.blog.constant.WebConstant;
import com.xiaojian.blog.controller.BaseController;
import com.xiaojian.blog.dto.LogAction;
import com.xiaojian.blog.dto.Types;
import com.xiaojian.blog.model.ContentPO;
import com.xiaojian.blog.model.ContentPOExample;
import com.xiaojian.blog.model.UserPO;
import com.xiaojian.blog.model.VO.RestResponseVO;
import com.xiaojian.blog.service.ContentService;
import com.xiaojian.blog.service.LogService;
import com.xiaojian.blog.service.MetaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;


/**
 * 页面控制器
 */
@Controller
@RequestMapping("admin/page")
public class PageController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PageController.class);

    @Resource
    private ContentService contentService;


    @Resource
    private LogService logService;

    @GetMapping(value = "")
    public String index(HttpServletRequest request) {
        ContentPOExample example = new ContentPOExample();
        example.setOrderByClause("created desc");
        example.createCriteria().andTypeEqualTo(Types.PAGE.getType());
        PageInfo<ContentPO> articlesWithpage = contentService.getArticlesWithpage(example, 1, WebConstant.MAX_POSTS);
        request.setAttribute("articles", articlesWithpage);
        return "admin/page_list";
    }

    /**
     * 页面编辑方法
     */
    @GetMapping(value = "/{cid}")
    public String editPage(@PathVariable String cid,
                           HttpServletRequest request) {
        ContentPO contents = contentService.getContents(cid);
        request.setAttribute("contents", contents);
        return "admin/page_edit";
    }

    /**
     * 添加新页面
     */
    @GetMapping(value = "/new")
    public String newPage(HttpServletRequest request) {
        return "admin/page_edit";
    }

    /**
     * 保存新页面
     */
    @PostMapping(value = "/publish")
    @ResponseBody
    public RestResponseVO publishPage(@RequestParam String title,
                                      @RequestParam String content,
                                      @RequestParam String status,
                                      @RequestParam String slug,
                                      @RequestParam(required = false) Integer allowComment,
                                      @RequestParam(required = false) Integer allowPing,
                                      HttpServletRequest request) {
        UserPO user = this.user(request);
        ContentPO contentPO = new ContentPO();
        contentPO.setTitle(title);
        contentPO.setContent(content);
        contentPO.setStatus(status);
        contentPO.setSlug(slug);//设置访问路径
        contentPO.setType(Types.PAGE.getType());
        if (null != allowComment) {
            contentPO.setAllowComment(allowComment == 1);
        }
        if (null != allowPing) {
            contentPO.setAllowPing(allowPing == 1);
        }
        //设置页面作者
        contentPO.setAuthorId(user.getUid());
        String result = contentService.publish(contentPO);
        if (!WebConstant.SUCCESS_RESULT.equals(result)) {
            return RestResponseVO.fail(result);
        }
        return RestResponseVO.ok();
    }

    /**
     * 删除页面
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public RestResponseVO delet(@RequestParam int cid, HttpServletRequest request) {
        String result = contentService.deleteByCid(cid);
        logService.insertLog(LogAction.DEL_PAGE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
        if (!WebConstant.SUCCESS_RESULT.equals(result)) {
            return RestResponseVO.fail(result);
        }
        return RestResponseVO.ok();
    }
}

